package chamans.away
{
	import away3d.containers.ObjectContainer3D;
	import away3d.entities.*;
	import away3d.events.*;
	import away3d.loaders.*;
	import away3d.loaders.parsers.*;
	import away3d.materials.*;
	import away3d.materials.methods.*;
	import away3d.primitives.*;
	
	import chamans.*;
	
	import flash.display.*;
	import flash.events.Event;
	import flash.geom.*;
	import flash.net.*;
	
	import jiglib.geometry.*;
	import jiglib.plugin.away3d4.*;
	
	public class barrel
	{
		private var BASE:Object
		private var num:int
		public var container:ObjectContainer3D
		public var meshListe:Vector.<Mesh> 
		public var matListe:Vector.<DefaultMaterialBase> 
		public var Fin:Function
		private var Pos:Vector3D
		
		public function barrel(base:Object, F:Function=null, N:int=0, pos:Vector3D=null)
		{
			BASE=base;
			num=N;
			if(!pos)Pos=new Vector3D(BASE.TOOL.Ran(1000),BASE.TOOL.Ran(80,50) , BASE.TOOL.Ran(1000))
			Fin=F;
			matListe = new Vector.<DefaultMaterialBase>();
			BASE.LOAD.IMGLIST("assets/demobullet/barrel/", ['exp_barrel', "exp_barrel_s", "exp_barrel_local" , "exp_barrel_white", "exp_barrel_red", "exp_barrel_blue"], initMaterials , ".jpg");
		}
		private function initMaterials( A:Array  ) : void
		{
			var mat0:BitmapMaterial = new BitmapMaterial(A[0]);
			var mat2:BitmapMaterial = new BitmapMaterial(A[3]);
			var mat3:BitmapMaterial = new BitmapMaterial(A[4]);
			var mat4:BitmapMaterial = new BitmapMaterial(A[5]);
			matListe.push(mat0);
			matListe.push(mat2);
			matListe.push(mat3);
			matListe.push(mat4);
			
			for each(var mat:BitmapMaterial in matListe){
				mat.lights = BASE.LIGHT;
				mat.ambientColor = BASE.FogColor;
				mat.specularMap = A[1];
				mat.normalMap = A[2];
				mat.ambient=.16;
				mat.gloss = 30;
				mat.specular = 1.25;
			}
			//add to root material list
			BASE.MatListe.push(mat0, mat2, mat3, mat4);
			meshListe = new Vector.<Mesh>();
			// load object
			BASE.OBJ3D('assets/demobullet/barrel/barrel.obj', OBJloaded);
		}
		public function OBJloaded(obj:ObjectContainer3D) : void
		{
			container=obj;
			BASE.view.scene.addChild(container);
			var mesh : Mesh;
			var i:int
			for (i = 0; i < container.numChildren; ++i) {
				mesh = Mesh(container.getChildAt(i));
				mesh.material=matListe[0]; 
				mesh.castsShadows = true; 
				if(i>0){ mesh.material=matListe[1];  mesh.castsShadows = false; }
				meshListe.push(mesh)
			}
			if(Fin!=null)Fin()
		}
		public function Del() : void
		{
			BASE.view.scene.removeChild(container);
		}
		public function clone(P:Vector3D=null) : void
		{   
			Pos= new Vector3D(BASE.TOOL.Ran(1000, -1000),BASE.TOOL.Ran(100,44) , BASE.TOOL.Ran(400, 800));
			if(P)Pos=P;
			var D:int=BASE.TOOL.Ran(3);
			var mesh:*=meshListe[0].clone();
			mesh.material=matListe[D];
			var _container:ObjectContainer3D = new ObjectContainer3D();
			_container.addChild(mesh);
			BASE.view.scene.addChild(_container);
			if(Fin!=null)Fin(mesh, Pos);
		}
		
	}}
